﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BLL;
using Model;
namespace UI
{
    public partial class 添加消费 : Form
    {
        private TabPage tp;

        public 添加消费()
        {
            InitializeComponent();
        }

        public 添加消费(TabPage tp, UI.主界面 主界面,Model_ConsumerBill m)
        {
            // TODO: Complete member initialization
            this.tp = tp;
            this.主界面 = 主界面;
            this.m = m;
            InitializeComponent();
        }
        public void CXps(string TJ)
        {
           
            listView2.Items.Clear();
            SqlDataReader dr = BLL_Products.CXsp(TJ);
            while (dr.Read())
            {
                string ProductID = dr["ProductID"].ToString();
                string ProductName = dr["ProductName"].ToString();
                string ProductPrice = dr["ProductPrice"].ToString();
                string PTName = dr["PTName"].ToString();
                ListViewItem li = new ListViewItem(ProductName);
                li.SubItems.Add(ProductPrice);
                li.SubItems.Add(PTName);
                li.Tag = ProductID;
                listView2.Items.Add(li);
            }
            dr.Close();
        }
        private void 添加消费_Load(object sender, EventArgs e)
        {
            CXps(null);
            ListView li = (ListView)tp.Controls[0];
            DataRow dr = (DataRow)li.SelectedItems[0].Tag;
            label3.Text = "（餐台号：" + dr["TableName"].ToString() + "）";
            CX_ZQ_DD();

            int shul = 0;
            double je = 0;
            foreach (ListViewItem r1 in listView1.Items)
            {
                shul += int.Parse(r1.SubItems[2].Text);
                je += double.Parse(r1.SubItems[3].Text);
            }
            label10.Text = shul.ToString();
            label11.Text = je.ToString();
        }

        public void CX_ZQ_DD() {
          listView1.Items.Clear();
          SqlDataReader dr= BLL_ConsumerDetails.ZQ_xfjr(m);
            while(dr.Read()){
                string ProductName = dr["ProductName"].ToString();
                string CDPrice = dr["CDPrice"].ToString();
                string CDAmount = dr["CDAmount"].ToString();
                string CDMoney = dr["CDMoney"].ToString();
                string CDDate = dr["CDDate"].ToString();
                string PTName = dr["PTName"].ToString();
                ListViewItem li = new ListViewItem(ProductName);
                li.SubItems.Add(CDPrice);
                li.SubItems.Add(CDAmount);
                li.SubItems.Add(CDMoney);
                li.SubItems.Add(CDDate);
                li.SubItems.Add(PTName);
                listView1.Items.Add(li);
            }
            dr.Close();
        }

        //每次输入一个字符进行条件查询
        string tjcx = null;
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 8)
            {
                try
                {
                   
                    if (tjcx.Length!=0)
                    {
                        tjcx = tjcx.Substring(0, tjcx.Length - 1);
                      
                       // CXps(tjcx);
                    }
                   
                  
                }
                catch (Exception)
                {

                    throw;
                }

            }
            else
            {
                tjcx += e.KeyChar.ToString();
               // CXps(tjcx);
            }

        }

        int num = 1;
        public void FZ()
        {
            //数量
            
            //判断是否有重复值
            int index = 0;
            bool F = true;
            foreach (ListViewItem item in listView2.Items)
            {

                foreach (ListViewItem r in listView1.Items)
                {
                    index++;

                    if (r.Text == listView2.SelectedItems[0].Text)
                    {

                        listView1.Items[index - 1].Remove();
                        F = false;

                       num=int.Parse( r.SubItems[2].Text);
                       ++num;
                    }

                }
            }

           
            UPXFJR();
            textBox2.Text = num.ToString();
            ListViewItem li = new ListViewItem(listView2.SelectedItems[0].SubItems[0].Text);
            li.SubItems.Add(listView2.SelectedItems[0].SubItems[1].Text);

            if (!F)
            {
                li.SubItems.Add(num.ToString());
               
            }
            else
            {
                li.SubItems.Add("1");
                TJXFJR();
           
            }
           

            li.SubItems.Add((int.Parse(textBox2.Text) * double.Parse(listView2.SelectedItems[0].SubItems[1].Text)).ToString());
            li.SubItems.Add(DateTime.UtcNow.ToString());
            li.SubItems.Add(listView2.SelectedItems[0].SubItems[2].Text);
            listView1.Items.Add(li);

             int shul=0;
                double je=0;
                 foreach (ListViewItem r1 in listView1.Items)
            {
                shul += int.Parse(r1.SubItems[2].Text);
                je += double.Parse(r1.SubItems[3].Text);
            }
            label10.Text = shul.ToString();
            label11.Text = je.ToString();
        }


        //项目清单的双击事件
        private 主界面 主界面;
        private Model_ConsumerBill m;
        private void listView2_DoubleClick(object sender, EventArgs e)
        {
            FZ();
            //添加消费记录
            主界面.FJ();

          
        }




        private void button3_Click(object sender, EventArgs e)
        {
            textBox2.Text = (int.Parse(textBox2.Text) + 1).ToString();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox2.Text == "1")
            { }
            else
            {
                textBox2.Text = (int.Parse(textBox2.Text) - 1).ToString();
            }
            
        }
        //添加消费记录
        public void TJXFJR()
        {
            //添加消费记录
            Model_ConsumerDetails md = new Model_ConsumerDetails();
            md.CBID = m.CBID;
            md.ProdcutID = listView2.SelectedItems[0].Tag.ToString();
            md.CDPrice = listView2.SelectedItems[0].SubItems[1].Text;
            md.CDAmount = textBox2.Text;
            md.CDSale = (int.Parse(md.CDPrice) * int.Parse(md.CDAmount) ).ToString();
            md.CDMoney = (int.Parse(md.CDPrice) * int.Parse(md.CDAmount)).ToString();
            BLL_ConsumerDetails.TJ_xfjr(md);
        }
        //修改消费记录
        public void UPXFJR() {
            Model_ConsumerDetails md = new Model_ConsumerDetails();
            md.CBID = m.CBID;
            md.ProdcutID = listView2.SelectedItems[0].Tag.ToString();
            md.CDPrice = listView2.SelectedItems[0].SubItems[1].Text;
            md.CDAmount = textBox2.Text;
            md.CDSale = (int.Parse(md.CDPrice) * int.Parse(md.CDAmount)).ToString();
            md.CDMoney = (int.Parse(md.CDPrice) * int.Parse(md.CDAmount)).ToString();
            BLL_ConsumerDetails.UP_SP(md);
        }
        private void button5_Click(object sender, EventArgs e)
        {


            if (textBox2.Text!="0")
	{
		 
	
            //判断是否有重复值
            int index = 0;
            bool F = true;
            foreach (ListViewItem item in listView2.Items)
            {

                foreach (ListViewItem r in listView1.Items)
                {
                    index++;

                    if (r.Text == listView2.SelectedItems[0].Text)
                    {


                        listView1.Items[index - 1].Remove();
                        F = false;
                    }

                }
            }
            if (F)
            {
                //添加消费记录
                TJXFJR();
            }
            //修改消费记录
            UPXFJR();
            
            ListViewItem li = new ListViewItem(listView2.SelectedItems[0].SubItems[0].Text);
            li.SubItems.Add(listView2.SelectedItems[0].SubItems[1].Text);
            li.SubItems.Add(textBox2.Text);
            li.SubItems.Add((int.Parse(textBox2.Text) * double.Parse(listView2.SelectedItems[0].SubItems[1].Text)).ToString());
            li.SubItems.Add(DateTime.UtcNow.ToString());
            li.SubItems.Add(listView2.SelectedItems[0].SubItems[2].Text);
            listView1.Items.Add(li);
            主界面.FJ();

                int shul=0;
                double je=0;
                 foreach (ListViewItem r1 in listView1.Items)
            {
                shul += int.Parse(r1.SubItems[2].Text);
                je += double.Parse(r1.SubItems[3].Text);
            }
            label10.Text = shul.ToString();
            label11.Text = je.ToString();
           
    }
            else
            {
                MessageBox.Show("请输入有效的数量");
            }

        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (char.IsNumber(e.KeyChar) || e.KeyChar == 8)
            {

            }
            else
            {
                e.Handled = true;
            }
        }

        private void listView2_Click(object sender, EventArgs e)
        {
            label2.Text = listView2.SelectedItems[0].SubItems[0].Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            宾客结账 b = new 宾客结账(tp, listView1, m, label11.Text);
            b.ShowDialog();
            this.Close();
        }

       
    }
}